<?php
/**
 * Constants including files
 *
 * <p>Defines the default php file extension type, and the default
 * directory separator.
 */
define('JUNCTION_EXTENSION', '.php');

/**
 * Include the file for a given class only if the class 
 * definition is absent
 * 
 * <p>This function assumes that the class names follow the
 * PEAR naming conventions.
 *
 * @param String $classname
 * @return void
 */
function using($classname) {
	if (class_exists($classname))
		return;
	$root = dirname(__FILE__);
	$path = str_replace("_", DIRECTORY_SEPARATOR, $classname);
	
	$fullpath = $root . DIRECTORY_SEPARATOR . strtolower(dirname($path)) . 
					DIRECTORY_SEPARATOR . basename($path) . JUNCTION_EXTENSION;

	require_once($fullpath);
}
/**
 * Includes an entire package if each class 
 * definition is absent.
 * 
 * <p>This works similar to import in java.
 * 
 * @param string package
 */
function package($package)
{
	$root = dirname(__FILE__);
	$path = str_replace("_", DIRECTORY_SEPARATOR, $package);
	
	$fullpath = $root . DIRECTORY_SEPARATOR . strtolower($path);
					
	if ($handle = opendir($fullpath)) {
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != ".." && !class_exists(str_replace(JUNCTION_EXTENSION, '', $package.'_'.$file))) {
				if(!is_dir($fullpath . DIRECTORY_SEPARATOR . $file)) {				
					require_once($fullpath . DIRECTORY_SEPARATOR . $file);
				}
			}
		}
		closedir($handle);
	}
}

/**
 * Include the specified file and ensure that it is only
 * included once.
 * 
 * <p>Note that the extension for the file should be omitted
 * and will be appended by the function.
 * <p>Use this function when you want to include a class but
 * it does not follow the PEAR naming convention.
 *
 * @param String $file fully qualified path to a file
 */
function get($file) {
	static $record = array();
	if (!$record[$file]) {
		require($file . JUNCTION_EXTENSION);
		$record[$file] = true;
	}
}

?>